package utils

import "gorm.io/gorm"

func BuildUserQuery(query *gorm.DB, req struct {
	ID         int    `form:"ID,omitempty"`
	Username   string `form:"Username,omitempty"`
	Email      string `form:"Email,omitempty"`
	Phone      string `form:"Phone,omitempty"`
	Department *int   `form:"Department,omitempty"` // 使用指针类型
	Avatar     string `form:"Avatar,omitempty"`
	PageSize   int    `form:"pageSize" binding:"required"`
	PageNum    int    `form:"pageNum" binding:"required"`
}) *gorm.DB {
	if req.ID > 0 {
		query = query.Where("id = ?", req.ID)
	}
	if req.Username != "" {
		query = query.Where("username LIKE ?", "%"+req.Username+"%")
	}
	if req.Email != "" {
		query = query.Where("email LIKE ?", "%"+req.Email+"%")
	}
	if req.Phone != "" {
		query = query.Where("phone LIKE ?", "%"+req.Phone+"%")
	}
	if req.Department != nil {
		query = query.Where("department = ?", *req.Department)
	}
	if req.Avatar != "" {
		query = query.Where("avatar = ?", req.Avatar)
	}
	return query
}
